<?php
// This controller handles the logic for the initiate auction page

class IniAuctionController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	
	/**
	 * Description: This method renders the buyer initiate auction page if the guest is logged in and is a buyer. If the user is not a buyer
	 * then the user will not be given access to the page. If the viewer is not logged in, then the application will redirect the view
	 * to the login page.
	 * Returns: Nothing
	 */
	public function actionBuyer() 
	{
		if(Yii::app()->user->isGuest)
		{
			echo "Please login first. We will redirect you to the login page in 3 secs";
			header("refresh:3;url=".Yii::app()->request->baseUrl);
		}
		else if(Yii::app()->user->userType=='s')
		{
			echo "you have no authority to view the buyer page";
		}
		else
		{
			$auction_model=new Auctions;
			$auction_model->scenario='create';
			$this->parseData($auction_model);
			$this->render('buyer', array('auction_model'=>$auction_model, ));
		}	
	}
	
	/**
	 * Description: This method validates the initiate auction data and submits on success
	 * Returns: Nothing
	 */
	public function parseData($auction_model)
	{
		if(isset($_POST['Auctions']))
		{
			$auction_model->user_ID=Yii::app()->user->userID;
			$auction_model->purchase_order_number=$_POST['Auctions']['purchase_order_number'];
			$auction_model->tank_ID=$_POST['Auctions']['tank_ID'];
			$auction_model->site_ID=$_POST['Auctions']['site_ID'];
			$auction_model->product_ID=$_POST['Auctions']['product_ID'];
			$auction_model->gallon=$_POST['Auctions']['gallon'];
			$auction_model->site_name=$_POST['Auctions']['site_name'];
			$auction_model->capacity=$_POST['Auctions']['capacity'];
			$auction_model->product_name=$_POST['Auctions']['product_name'];
			$auction_model->inventory_last_read=$_POST['Auctions']['inventory_last_read'];
			$auction_model->inventory_last_time=$_POST['Auctions']['inventory_last_time'];

			if($auction_model->gallon!=null&&$auction_model->tank_ID>0)
			{
				if($auction_model->gallon<=0||$auction_model->gallon>Tanks::model()->findByPk($auction_model->tank_ID)->capacity)
				{
					$auction_model->addError('gallon', 'The gallon amount must be greater than 0 and cannot be greater than the tank\'s capacity.');
					return;
				}
			}
			$auction_model->status_ID=1;
			$auction_model->auction_start=trim($_POST['begindate'])." ".trim($_POST['starttime']).":00";
			$auction_model->auction_end=trim($_POST['enddate'])." ".trim($_POST['endtime']).":00";
			$auction_model->delivery_due=trim($_POST['duedate'])." ".trim($_POST['duetime']).":00";
			$auction_model->notes=$_POST['Auctions']['notes'];
			if($auction_model->save())
			{
				$this->redirect(Yii::app()->request->baseUrl . '/auctions/buyer');
			}
		}
	}
	
	/**
	 * Description: This method makes an ajax call to retrieve tank details from the database
	 * Returns: The tank record
	 */
	public function actionTankInfo()
	{
		if(Yii::app()->request->isAjaxRequest)
		{
			$data=Tanks::model()->getTankInfo($_POST['Auctions']['tank_ID']);
			echo CJSON::encode($data);
		}
	}
}
?>
